/*
 * morestdio.h
 *
 *  Created on: Sep 9, 2008
 *      Author: lindenb
 */
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <bits/stdio_lim.h>
#include "lang/morestdlib.h"
#include "lang/morestring.h"
#include "lang/morestdio.h"
char* dirname(const char* path)
	{
	int i =strLastIndexOfString(path,FILE_pathSeparator);
	return i==-1?
			NULL:
			substr(path,0,i-1);
	}

int fileExists(const char* path)
	{
	FILE* in;
	if(path==NULL) return 0;
	in= fopen(path,"r");
	if(in==NULL) return 0;
	fclose(in);
	return 1;
	}

FilePtr file_new()
    {
    FilePtr p= (FilePtr)Calloc(1,sizeof(File));
    return p;
    }

FILE* file_open(FilePtr ptr,const char* filename,const char* mode)
    {
    assert(ptr!=NULL);
    if(ptr->io!=NULL)
        {
        fflush(ptr->io);
        fclose(ptr->io);
        }
    ptr->filename[0]=0;
    ptr->io=NULL;
    if(filename!=NULL && mode!=NULL)
        {
        ptr->io=fopen(filename,mode);
        if(ptr->io!=NULL) strncpy(ptr->filename,filename,FILENAME_MAX);
        }
    return ptr->io;
    }

void file_delete(FilePtr ptr)
    {
    if(ptr==NULL) return;
    Free(ptr);
    }
